package com.fruitbasket.audioplatform;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ContextUtil extends Application {
    private static Context context;
    public static String tmpfile;
    public static String LOG_TAG = "xianjian";
    @Override
    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

        /*************************************************************************/
        try {
            String assetsDir = getExternalFilesDir(null).toString();
            unZip("ele.zip", assetsDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Context getContext(){
        return context;
    }

    /*
     * filename object filename, resId resource id
     * */
    void createExternalStoragePrivateFile(String filename, int resId) {
        // Create a path where we will place our private file on external
        // storage.
        File file = new File(getExternalFilesDir(null), filename);

        try {
            // Very simple code to copy a picture from the application's
            // resource into the external file.  Note that this code does
            // no error checking, and assumes the picture is small (does not
            // try to copy it in chunks).  Note that if external storage is
            // not currently mounted this will silently fail.
            @SuppressLint("ResourceType") InputStream is = getResources().openRawResource( resId );
            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read(data);
            os.write(data);
            is.close();
            os.close();
        } catch (IOException e) {
            // Unable to create file, likely because external storage is
            // not currently mounted.
            Log.w("ExternalStorage", "Error writing " + file, e);
        }
    }

    void deleteExternalStoragePrivateFile(String filename) {
        // Get path for the file on external storage.  If external
        // storage is not currently mounted this will fail.
        File file = new File(getExternalFilesDir(null), filename);
        file.delete();
    }

    boolean hasExternalStoragePrivateFile(String filename) {
        // Get path for the file on external storage.  If external
        // storage is not currently mounted this will fail.
        File file = new File(getExternalFilesDir(null), filename);
        return file.exists();
    }

    /**
     * @param assetName
     * @param outputDirectory
     * @throws IOException
     */
    public void unZip(String assetName,String outputDirectory) throws IOException {

        File file = new File(outputDirectory);
        if (!file.exists()) {
            file.mkdirs();
        }

        InputStream inputStream = null;
        inputStream = getAssets().open(assetName);
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        byte[] buffer = new byte[1024];
        int count = 0;

        while (zipEntry != null) {
            if (zipEntry.isDirectory()) {
                file = new File(outputDirectory + File.separator + zipEntry.getName());
                file.mkdir();
            } else {
                file = new File(outputDirectory + File.separator + zipEntry.getName());
                if(file.delete()){
                    file.createNewFile();
                }else{
                    file.createNewFile();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                while ((count = zipInputStream.read(buffer)) > 0) {
                    fileOutputStream.write(buffer, 0, count);
                }
                fileOutputStream.close();
            }
            zipEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
    }
}
